package com.qixin.unipush;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description: TODO
 * @author: wengmf
 * @date: 2022年03月29日11:05
 */
@Component
@Slf4j
public class GeTuiPushV2 {

    // 应用配置信息
    private static final String appId = "vlxpjqjeZP7nKBYhmcOdj5";
    private static final String appKey = "R16ghzmG5T6Rqi5aN8gvc2";
    private static final String mastersecret = "lqCzgf99ni58jvaqkj2f79";

    private PushApi pushApi;

    public PushApi getPushApi() {
        if (pushApi == null){
            GtApiConfiguration apiConfiguration = new GtApiConfiguration();
            //填写应用配置
            apiConfiguration.setAppId(appId);
            apiConfiguration.setAppKey(appKey);
            apiConfiguration.setMasterSecret(mastersecret);
            apiConfiguration.setDomain("https://restapi.getui.com/v2/");
            // 实例化ApiHelper对象，用于创建接口对象
            ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
            // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
            pushApi = apiHelper.creatApi(PushApi.class);
        }
        return pushApi;
    }

    public void pushToSingleByCid(LoginUser toUser, String title, String content){

        if (StringUtils.isBlank(toUser.getClientId())){
            return;
        }

        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");//requestid需要每次变化唯一
        //配置推送条件
        // 1: 表示该消息在用户在线时推送个推通道，用户离线时推送厂商通道;
        // 2: 表示该消息只通过厂商通道策略下发，不考虑用户是否在线;
        // 3: 表示该消息只通过个推通道下发，不考虑用户是否在线；
        // 4: 表示该消息优先从厂商通道下发，若消息内容在厂商通道代发失败后会从个推通道下发。
        Strategy strategy = new Strategy();
        strategy.setDef(1);
        Settings settings = new Settings();
        settings.setStrategy(strategy);
        pushDTO.setSettings(settings);
        settings.setTtl(3600000);//消息有效期，走厂商消息需要设置该值
        //安卓离线厂商通道推送消息体
        PushChannel androidPushChannel = new PushChannel();
        AndroidDTO androidDTO = new AndroidDTO();
        Ups ups = new Ups();
        ThirdNotification notification1 = new ThirdNotification();

        ups.setNotification(notification1);
        // 标题
        notification1.setTitle(title);
        // 内容
        notification1.setBody(content);
        notification1.setClickType("intent");
        notification1.setIntent("intent://io.dcloud.unipush/?#Intent;scheme=unipush;launchFlags=0x4000000;component=" +
                                "com.fjjmrh.fybx/io.dcloud.PandoraEntry;S.UP-OL-SU=true;end");
        //各厂商自有功能单项设置
        //ups.addOption("HW", "/message/android/notification/badge/class", "io.dcloud.PandoraEntry ");
        //ups.addOption("HW", "/message/android/notification/badge/add_num", 1);
        //ups.addOption("HW", "/message/android/notification/importance", "HIGH");
        //ups.addOption("VV","classification",1);
        androidDTO.setUps(ups);
        androidPushChannel.setAndroid(androidDTO);
        pushDTO.setPushChannel(androidPushChannel);

        // PushMessage在线走个推通道才会起作用的消息体
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("title", title);
        jsonObject.put("content", content);
        jsonObject.put("payload", "test");
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTransmission(JSON.toJSONString(jsonObject));
        pushDTO.setPushMessage(pushMessage);

        // 设置接收人信息
        Audience audience = new Audience();
        audience.addCid(toUser.getClientId());
        pushDTO.setAudience(audience);

        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = getPushApi().pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            // success
           log.info("消息推送成功：" + apiResult.getData().toString());
        } else {
            // failed
            log.error("消息推送失败，code:{},msg:{}",apiResult.getCode(),apiResult.getMsg() );
//            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }

    }

}
